package com.xqx.controller;
import	java.awt.Rectangle;
import	java.security.Policy;

import com.github.pagehelper.PageInfo;
import com.xqx.pojo.Student;
import com.xqx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;;
import java.util.List;

/**
 * @author：xingquanxiang createTime：2019/10/26 15:24
 * description:
 */
@Controller
@RequestMapping(value = "/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/findByPage")
    @ResponseBody
    public PageInfo<Student> findAll(int pageNum, int pageSize){
        return studentService.findAll(pageNum, pageSize);
    }

    @PostMapping("/add")
    @ResponseBody
    public Student add(Student student) throws ParseException {
        return studentService.doAdd(student);
    }

    @PostMapping("/edit")
    @ResponseBody
    public boolean edit(@RequestBody Student student){
        System.out.println("student = " + student);
        return studentService.doEdit(student);
    }


    @PostMapping("/del")
    @ResponseBody
    public int del(long[] id){
        return studentService.doDel(id);
    }

    @PostMapping(value = "/login")
    public String login(Student student, HttpSession session){
        String targetUri = (String)session.getAttribute("targetUri");
        System.out.println(targetUri);
        if (studentService.findByNameAndPassword(student)){
            session.setAttribute("name","xqx");
            session.setAttribute("password","1998");
            // return !targetUri.isEmpty()&&targetUri.length()>0 ? "redirect:"+targetUri.substring(targetUri.lastIndexOf("/")):"index";
            return "redirect:/index_vue.html";
        }else{
            return "redirect:/error.html";
        }
    }

    @GetMapping("/findOne")
    @ResponseBody
    public Student findOne(long id){
        return studentService.findById(id);
    }


}
